package personal.xjl._23java2studentdemo.controller;

import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import personal.xjl._23java2studentdemo.mapper.TeacherMapper;
import personal.xjl._23java2studentdemo.model.Course;
import personal.xjl._23java2studentdemo.model.Student;
import personal.xjl._23java2studentdemo.model.Teacher;
import personal.xjl._23java2studentdemo.service.CourseService;
import personal.xjl._23java2studentdemo.service.TeacherService;
import personal.xjl._23java2studentdemo.service.impl.CourseServiceImpl;

import java.util.List;

@Controller
@RequestMapping("/Teacher")
public class TeacherController {
    @Autowired
//    TeacherMapper teacherMapper;
    TeacherService teacherService;
    @Autowired
    private CourseServiceImpl courseServiceImpl;

    @RequestMapping("list")
    public String list(Model model) {
        List<Teacher> teachers=teacherService.findAll();
        model.addAttribute("teachers",teachers);
        return "listTeachers";
    }

    @RequestMapping("toPage")
    public String toPage(String page){
        return page;
    }

    //删除老师
    @RequestMapping("remove")
    public String remove(int id){
        teacherService.remove(id);
        //重定向到List方法，刷新数据，重新select，返回给用户看
        return "redirect:/Teacher/list";
    }

    //新增教师
    @RequestMapping("add")
    public String add(Teacher teacher){
        //调用service的增加方法，返回到显示页面
        teacherService.add(teacher);
        return "redirect:/Teacher/list";
    }

    @Autowired
//    TeacherMapper teacherMapper;
    CourseService courseService;

    @RequestMapping("toAdd")
    public String toAdd(Model model){
        List<Course> courses= courseService.findAll();
        model.addAttribute("courses", courses);
        return "addTeacher";
    }

    //修改
    @RequestMapping("edit")
    public String edit(Teacher teacher){
        teacherService.edit(teacher);
            return "redirect:/Teacher/list";
    }
    @RequestMapping("toEdit")
    public String toEdit(int id, Model model){
        Teacher teacher = teacherService.findOneById(id);
        List<Course> courses = courseService.findAll();

        model.addAttribute("teacher", teacher);
        model.addAttribute("courses", courses);
        return "editTeacher";
    }

    //分页显示
//    @RequestMapping("list/{currPage}/{pageSize}")
//    public String list(Model model, @PathVariable("currPage") int currPage, @PathVariable("pageSize") int pageSize){
//        if(currPage<1) currPage=1;
//        if(pageSize<1) pageSize=5;
//        List<Teacher> teachers = teacherService.findByPage(currPage, pageSize);
//        model.addAttribute("teachers", teachers);
//        return "listTeachers";
//    }
    @RequestMapping("list/{currPage}/{pageSize}")
    public String list(Model model, @PathVariable("currPage") int currPage, @PathVariable("pageSize") int pageSize) {
        if (currPage < 1) currPage = 1;
        if (pageSize < 1) pageSize = 5;
        List<Teacher> teachers = teacherService.findByPage(currPage, pageSize);
        model.addAttribute("teachers", teachers);
        model.addAttribute("currPage", currPage);
        return "listTeachers";
    }

//    @RequestMapping("loginValidate")
//    public String loginValidate(String username, String password, Model model, HttpSession session) {
//        //登录判断
//        if(teacherService.login(username,password)) {
//            //增加一个session有效的变量（令牌），拦截器的通关检查
//            session.setAttribute("username", username);
//            return "redirect:/Teacher/list/1/3";
//        }
//        else
//            return "redirect:/Teacher/toPage?page=login";
//    }
//
//    @RequestMapping("login")
//    public String login(){
//        return "login";
//    }
//
//    @RequestMapping("logout")
//    public String logout(HttpSession session) {
//        // 清除 session 中的用户信息
//        session.removeAttribute("username");
//        // 重定向到登录页面
//        return "redirect:/Teacher/login";
//    }
}
